﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// Simple Service Administration Tool for WinNT based systems.
// Copyright © 2010 - Silvan Gehrig
// 
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// 
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
// 
// Author:
//  Silvan Gehrig
//
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using SSATool.BL.DM;
using SSATool.Common.Util;
using System.ComponentModel;

#endregion

namespace SSATool.UI.ViewModel
{
    /// <summary>
    /// Represents a collection of service instances.
    /// </summary>
    public class DataServiceCollection : ObservableCollection<DataService>
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        private readonly ServiceCollection _domainCollection;

        private bool _eventAdded = false;
        private bool _innerEventsEnabled = true;
        private NotifyCollectionChangedEventHandler _onCollectionChanged;

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        /// <summary>
        /// Gets the service with the given name.
        /// </summary>
        /// <param name="serviceName">Name of the service to retrieve.</param>
        /// <returns>Returns the retrieved service or fires an exception if it could not be found.</returns>
        public DataService this[string serviceName]
        {
            get { return new DataService(this, _domainCollection[serviceName]); }
        }

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        /// <summary>
        /// Initializes a new instance of the <see cref="DataServiceCollection"/> class.
        /// </summary>
        /// <param name="domainCollection">Specifies the parent domain collection instance.</param>
        public DataServiceCollection(ServiceCollection domainCollection)
        {
            PreCondition.AssertNotNull(domainCollection, "domainCollection");

            _domainCollection = domainCollection;
        }

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        /// <summary>
        /// Determines whether the current collection contains the specified item.
        /// </summary>
        /// <param name="serviceName">The name of the service to check.</param>
        /// <returns>
        /// 	<c>true</c> if the current collection contains the specified item; otherwise, <c>false</c>.
        /// </returns>
        public bool Contains(string serviceName)
        {
            PreCondition.AssertNotNull(serviceName, "serviceName");
            return _domainCollection.Contains(serviceName);
        }

        /// <summary>
        /// Refreshes the data with the underlying data source.
        /// </summary>
        public void Reset()
        {
            CallWithoutInnerEvents(() => _domainCollection.Reset());
            Clear();

            foreach (Service service in _domainCollection)
            {
                Items.Add(new DataService(this, service));
            }
            OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
        }

        /// <summary>
        /// Fires the ChildPropertyChanged event.
        /// </summary>
        /// <param name="sender">Specifies the sender service instance.</param>
        /// <param name="args">Event args to pass to the receivers.</param>
        internal void FireChildChanged(DataService sender, PropertyChangedEventArgs args)
        {
            if (ChildChanged != null)
                ChildChanged(sender, args);
        }

        private void CallWithoutInnerEvents(Action toExecute)
        {
            try
            {
                _innerEventsEnabled = false;
                toExecute();
            }
            finally
            {
                _innerEventsEnabled = true;
            }
        }

        private void EnsureCollectionChangedEventAdded()
        {
            if (!_eventAdded)
            {
                _domainCollection.CollectionChanged += OnDomainCollectionCollectionChanged;
                _eventAdded = true;
            }
        }

        private void EnsureCollectionChangedEventRemoved()
        {
            if (_onCollectionChanged != null && _eventAdded)
            {
                _domainCollection.CollectionChanged -= OnDomainCollectionCollectionChanged;
                _eventAdded = false;
            }
        }

        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------

        /// <summary>
        /// Occurs when a child item mutated.
        /// </summary>
        public event PropertyChangedEventHandler ChildChanged;

        /// <summary>
        /// Occurs when an item is added, removed, changed, moved, or the entire list is refreshed.
        /// </summary>
        public event NotifyCollectionChangedEventHandler Changed
        {
            add
            {
                _onCollectionChanged += value;
                EnsureCollectionChangedEventAdded();
            }
            remove
            {
                _onCollectionChanged -= value;
                EnsureCollectionChangedEventRemoved();
            }
        }

        /// <summary>
        /// Occurs when an item is added, removed, changed, moved, or the entire list is refreshed.
        /// </summary>
        public override event NotifyCollectionChangedEventHandler CollectionChanged
        {
            add
            {
                _onCollectionChanged += value;
                EnsureCollectionChangedEventAdded();
            }
            remove
            {
                _onCollectionChanged -= value;
                EnsureCollectionChangedEventRemoved();
            }
        }

        private void OnDomainCollectionCollectionChanged(object sender, ServiceCollectionChangedEventArgs e)
        {
            if (!_innerEventsEnabled)
                return;

            switch (e.Action)
            {
                case ServiceCollectionChangedAction.Add:
                    Add(new DataService(this, e.ChangedItem));
                    break;
                case ServiceCollectionChangedAction.Remove:
                    Remove(new DataService(this, e.ChangedItem));
                    break;
                case ServiceCollectionChangedAction.Reset:
                    Reset();
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }
        }

        #endregion
    }
}
